package com.wsd.dao.impl;

import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Query;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.wsd.bean.Category;
import com.wsd.bean.Products;
import com.wsd.dao.CategoryDaoInterface;

/**
 * A data access object (DAO) providing persistence and search support for
 * Category entities. Transaction control of the save(), update() and delete()
 * operations can directly support Spring container-managed transactions or they
 * can be augmented to handle user-managed Spring transactions. Each of these
 * methods provides additional information for how to configure it for the
 * desired type of transaction control.
 * 
 * @see com.wsd.bean.Category
 * @author MyEclipse Persistence Tools
 */

public class CategoryDAO extends HibernateDaoSupport implements CategoryDaoInterface {
	private static final Log log = LogFactory.getLog(CategoryDAO.class);

	protected void initDao() {
		// do nothing
	}

	public void save(Category transientInstance) {
		log.debug("saving Category instance");
		try {
			getHibernateTemplate().save(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}

	public void delete(Category persistentInstance) {
		log.debug("deleting Category instance");
		try {
			getHibernateTemplate().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	public Category findById(java.lang.Integer id) {
		log.debug("getting Category instance with id: " + id);
		try {
			Category instance = (Category) getHibernateTemplate().get(
					"com.wsd.bean.Category", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}
	
	public List findByExample(Object exampleEntity) {
		log.debug("finding by Products instances");
		try {
			return getHibernateTemplate().findByExample(exampleEntity);
		} catch (RuntimeException re) {
			log.error("find example failed", re);
			throw re;
		}
	}

	@SuppressWarnings("unchecked")
	public List<Category> findAll() {
		log.debug("finding all Category instances");
		try {
			String queryString = "from Category";
			return getHibernateTemplate().find(queryString);
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	@SuppressWarnings("unchecked")
	public List<Category> findByLevel(int level) {
		log.debug("finding all Category instances");
		try {
			String queryString = "from Category where CLevel = ?";
			Query query = getHibernateTemplate().getSessionFactory().getCurrentSession().createQuery(queryString);
			query.setInteger(0, level);
			return query.list();
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	public void persist(Object entity){
		getHibernateTemplate().persist(entity);
		getHibernateTemplate().flush();
	}

	public void update(Category category) {
		getHibernateTemplate().update(category);
	}

	/* (non-Javadoc)
	 * @see com.wsd.dao.CategoryDaoInterface#updateGt(int, int)
	 */
	public void updateGt(int rgt, int num) {
		String sql1 = "update Category set lft = lft+:num where lft >=:rgt";
		String sql2 = "update Category set rgt = rgt + :num where rgt>=:rgt";
		getHibernateTemplate().getSessionFactory().getCurrentSession().createQuery(sql1).setInteger("rgt", rgt).setInteger("num", num).executeUpdate();
		getHibernateTemplate().getSessionFactory().getCurrentSession().createQuery(sql2).setInteger("rgt", rgt).setInteger("num", num).executeUpdate();
	}
}